<?php
class WxUserController extends WechatAppController{
	
	public function beforeFilter(){
	    $this->Auth->allow('bind','valid');
		parent::beforeFilter();
	}

	public function loginState(){
	    $this->layout = 'ajax';
	    if($this->Auth->loggedIn()){
	      $state = true;  
	      $msg = 'login';
	    }else{
	      $state = false;
	      $msg = 'logout';
	    }
	}
	
    public function bind() {
        $this->loadModel('User');
        if($this->Auth->loggedIn()){
            $userLogin = $this->User->findById($this->Auth->user('id'));
            if($userLogin){
                $this->Auth->login($userLogin['User']);
            } 
            $this->redirect(array('plugin'=>'wechat','controller'=>'WxUser','action'=>'switcher'));
        }else{
            $this->loadModel('Wechat.WxUser');
            $link = array('plugin'=>'wechat','controller'=>'WxUser','action'=>'valid');
            $link = $this->WxUser->getOauthLink($link);
            $this->redirect($link);
        }
	}
	
	public function valid(){
	    $this->autoRender =false;
	    if (!isset($_GET['code'])) {
	       echo '用户没有授权,无法操作';   
	    }else{
    	    $this->loadModel('Wechat.WxUser');
	        $res = $this->WxUser->access_token($_GET['code']);
	        $this->loadModel('User');
	        if($res){
	            $access_tocken =@json_decode($res,true);
	            if(isset($access_tocken['openid'])){
	                $openid = $access_tocken['openid'];
	                $check = $this->User->findByext_wx_openid($openid);
	                if($check){ //已有用户 登录
	                    $this->Auth->login($check['User']);
	                }else{ //没有用户拉取用户信息 注册
	                    $userinfo = $this->WxUser->userinfo($access_tocken['access_token'],$access_tocken['openid']);
	                    $userinfo = @json_decode($userinfo,true);
	                    $user = array();
	                    $user['User']['ext_wx_openid'] = $userinfo['openid'];
	                    $user['User']['name'] = $userinfo['nickname'];
	                    $user['User']['sex'] = $userinfo['sex']==1?1:0;
	                    $user['User']['password'] = '';
	                    if(!$this->User->register($user,2,false)){
	                        echo 'registe error try again';      
	                        exit;           
	                    }else{
	                        $check = $this->User->findByext_wx_openid($userinfo['openid']);
                            $this->Auth->login($check['User']);
	                    }
	                }
		            $this->redirect(array('plugin'=>'wechat','controller'=>'WxUser','action'=>'switcher'));
	            }else{
	            	echo $access_tocken['errmsg'];
	            }
	            //$this->redirect(array('plugin'=>false,'controller'=>'Index','action'=>'index'));
	        }else{
	            echo 'access_token error';
	        }
	    }
	}
	
	public function switcher(){
	    if($this->Session->check('wxjump')){
	    	$link = $this->Session->read('wxjump');
	        $this->Session->delete('wxjump');
	        $this->redirect($link);
	    }else{
	        $this->redirect(array('plugin'=>false,'controller'=>'Index','action'=>'wechat'));
	    }
	}
	
	public function subornot(){
		$this->loadModel('Wechat.WxSubscribe');
		if($this->WxSubscribe->substate($this->Auth->user('openid'))){
			if($this->Session->check('wxorder')){
				$this->Session->delete('wxorder');
				$this->redirect(array('plugin'=>'order','controller'=>'Orders','action'=>'mine'));
			}elseif($this->Session->check('wxaddress')){
				$this->Session->delete('wxaddress');
				$this->redirect(array('plugin'=>'address','controller'=>'UserAddresses','action'=>'index'));
			}elseif($this->Session->check('wxticket')){
			    $this->Session->delete('wxticket');
			    $this->redirect(array('plugin'=>'ticket','controller'=>'Tickets','action'=>'index'));
			}elseif($this->Session->check('wxjump')){
			    $url = $this->Session->read('wxjump');
			    $this->Session->delete('wxjump');
			    $this->redirect($url);
			}else{
				$this->redirect(array('plugin'=>false,'controller'=>'Index','action'=>'index'));
			}
		}else{
			$this->Auth->logout();
			$this->redirect('http://mp.weixin.qq.com/s?__biz=MzA5MzEwNzcwNA==&mid=400265648&idx=1&sn=5c34d692b0107423d052c5ee1ca3554f#rd');
		}
		//exit;	
		
	}
}
